package com.young.common.context;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @description：
 * @author: yangyahui01
 * @date: 11/29/24 2:13 PM
 */
public final class Profile {
    private static final String SEPARATOR = "@";
    private static final Map<String, String> SM = new HashMap();
    private static final Map<String, Profile> SP = new HashMap();
    public static final Profile PROD;
    public static final Profile PRE;
    public static final Profile BETA;
    public static final Profile TEST;
    public static final Profile DEV;
    public static final Profile DEFAULT;
    private String facade;
    private String standard;
    private String child;
    private String parent;

    public String getFacade() {
        return this.facade;
    }

    public String getStandard() {
        return this.standard;
    }

    public String getChild() {
        return this.child;
    }

    public String getParent() {
        return this.parent;
    }

    public boolean isStandard() {
        return this.standard != null;
    }

    private Profile(String facade) {
        this.facade = facade;
        String[] segments = facade.split("@");
        this.parent = segments.length >= 2 ? segments[1] : segments[0];
        this.child = segments.length >= 2 ? segments[0] : null;
        if (SM.containsKey(this.parent)) {
            this.standard = (String)SM.get(this.parent);
        }

    }

    public static Profile profileOf(String profile) {
        if (profile == null) {
            return null;
        } else {
            String lc = profile.toLowerCase();
            String sn = (String)SM.get(lc);
            return sn != null ? (Profile)SP.get(sn) : new Profile(lc);
        }
    }

    public String prefer() {
        return this.isStandard() ? this.standard : this.facade;
    }

    public static Map<String, Profile> standards() {
        return Collections.unmodifiableMap(SP);
    }

    public static Profile standard(String profile) {
        if (profile == null) {
            return null;
        } else {
            String lc = profile.toLowerCase();
            String sn = (String)SM.get(lc);
            return (Profile)SP.get(sn);
        }
    }

    static {
        SM.put("production", "prod");
        SM.put("prod", "prod");
        SM.put("preonline", "pre");
        SM.put("pre", "pre");
        SM.put("beta", "beta");
        SM.put("test", "test");
        SM.put("development", "dev");
        SM.put("dev", "dev");
        SM.put("default", "default");
        PROD = new Profile("prod");
        PRE = new Profile("pre");
        BETA = new Profile("beta");
        TEST = new Profile("test");
        DEV = new Profile("dev");
        DEFAULT = new Profile("default");
        SP.put("prod", PROD);
        SP.put("pre", PRE);
        SP.put("beta", BETA);
        SP.put("test", TEST);
        SP.put("dev", DEV);
        SP.put("default", DEFAULT);
    }
}

